<?php

/**
 * Created by PhpStorm.
 * User: shwy
 * Date: 2017/5/18
 * Time: 15:55
 * 用户管理模块
 */

class User extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('user_model');
        $this->load->model('role_model');
        $this->load->model('procurement_model');
        $this->load->model('returnOrder_model');
        $this->load->model('supplier_model');
        $this->load->model('stockOut_model');
        $this->load->model('stockPut_model');

		$username = $this->session->userdata('username');
		$userid   = $this->session->userdata('userid');
		
		if (!$username || !$userid) {
			redirect('admin/login/index');
		}
    }

    public function index()
    {
		// 只有管理员权限才能访问
        if ($this->session->userdata('username') !== 'admin') {
        	die('抱歉你没权限访问此页面');
			redirect('home/index');
        }

        $page_num = $this->config->item('fenye')['pageNum']; //每页的数据
        $start_position = 0; //初始数据
        $page = $this->input->get('page'); //获取页码数量
        if($page){
            $start_position = ($page - 1) * $page_num;
        }

        //判断搜索内容是否存在存在就继续
        $data = $this->user_model->page('user',0,$page_num,$start_position); //limit查询的数据

        if($data[0]){
            $viewData = [
                'list' => $data[0],  //这里得到从数据库中的当页数据
                'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                'pageNum'  => $page_num //每页显示多少条
            ];
        }else{
            $viewData = [
                'list' => '',  //这里得到从数据库中的当页数据
                'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                'pageNum'  => $page_num //每页显示多少条
            ];
        }


        $viewData['roleName'] = $this->role_model->getData('id,name',1);



//            echo '<pre>';
//            print_r($viewData);
//            die();

        $this->load->view('admin/user/list',$viewData);
    }

    //2017-5-21 11:56:01  数据库添加完成
    //添加功能
    public function add(){
        // 查询username和nickname是否已经存在
        $arg = [
            'username' => $this->input->post('username')
        ];

        $username = $this->user_model->getByFieldQueryData('*',$arg,0);

        $arg = [
            'nickname' => $this->input->post('nickname')
        ];

        $nickname = $this->user_model->getByFieldQueryData('*',$arg,0);

        if($username || $nickname){
            echo __alert('用户名或者登录名已存在！');
            die;
        }

        // 根据直接存储role_id , name
        $data = [
            'username' => $this->input->post('username'),   //登录名
            'nickname' => $this->input->post('nickname'),   //显示的昵称
            'password' => $this->input->post('password'),   //密码
            'role_id' => $this->input->post('role_id'),     //权限id
            'sex' => $this->input->post('sex'),
            'phone' => $this->input->post('phone'),
            'section' => $this->input->post('section'),     //部门
            'residence' => $this->input->post('residence'), //地址
            'card' => $this->input->post('card'),           //身份证
            'loginTime' => time(),
            'createTime' => time()
        ];

        //是否会有添加失败的情况?

        $resultData =  $this->user_model->addId($data);

        if($resultData){
            redirect('/admin/user/index?page=1');
        }else{
            echo __alert('参数错误');
        }
    }

    // 添加页面
    public function addView()
    {
        $this->load->view('/admin/user/add');
    }

    // 编辑功能
    public function edit()
    {
        //用户id
        $where = [
            'id' => $this->input->post('id')
        ];
        $data = [
            'username' => $this->input->post('username'),
            'nickname' => $this->input->post('nickname'),
            'password' => $this->input->post('password'),
            'role_id' => $this->input->post('role_id'),
            'sex' => $this->input->post('sex'),
            'phone' => $this->input->post('phone'),
            'section' => $this->input->post('section'),
            'residence' => $this->input->post('residence'),
            'card' => $this->input->post('card'),
            'updateTime' => time()
        ];

        //是否会有添加失败的情况?
        $resultData =  $this->user_model->edit($data,$where);

        if($resultData){
            echo '<script>alert("修改成功")</script>';
            redirect('/admin/user/index');
        }

    }

    // 编辑页面
    public function editView(){

        // 读取用户名根据get id
        $arg = [
            'id' => $this->input->get('id')
        ];

        $data['list'] = $this->user_model->getByFieldQueryData('*',$arg,0);

        // 如果不存在直接跳转到404页面
        if (!$data['list']) {
            redirect('/home/error');
        }
        $this->load->view('/admin/user/edit',$data);
    }

    // 批量删除
    public function remove(){
        $postData = $this->input->post();
        if($postData){
            if(!$postData){
                redirect('/admin/user/index?page=1');
            }

            $post = $this->input->post('del_id');
            foreach ($post as $v){
                $where = [
                    'id' => $v
                ];

                $this->user_model->remove($where);
            }
            __logInfo('用户删除信息：',$_COOKIE.'删除了'.$this->input->get('id').'账号');
            redirect('/admin/user/index?page=1');

        }else{
            $data = [
                'id' => $this->input->get('id'),
            ];

            //是否会有添加失败的情况?
            $resultData =  $this->user_model->remove($data);
            if($resultData){
                __logInfo('用户删除信息：',$_COOKIE.'删除了'.$this->input->get('id').'账号');
                redirect('/admin/user/index?page=1');
            }
        }

    }


    // 一级审批人接口
    public function oneLevel() {   
		// 查询参数
        $searchArg = [
            'field' => 'role_id',
            'arg'=> '主管',
            'side' => ''
        ];
		// 返回的是数组
        $result = $this->user_model->search('id,nickname', 0, $searchArg, 0, 0);
		// 用json_encode把数组用json格式表示
		// 参数JSON_UNESCAPED_UNICODE: Json不要编码Unicode, 让中文字符在json_encode的时候不用转义
        echo json_encode($result[0], JSON_UNESCAPED_UNICODE);
    }
	
	/**
	 * 功能: 二级审批人接口, 二级审批人数据json返回
	 * 
	 * @param $fields 查询字段
	 * @param $where  查询条件
	 * @param $bool	    是否查询多条, 默认为多条
	 */
    public function twoLevel() {
    	$fields = 'id, nickname';
		$where  = [
					'role_id' => '保障中心总监(副)'
				];
    	$result = $this->user_model->findData($fields, $where); 
        echo json_encode($result, JSON_UNESCAPED_UNICODE);
    }

    // 三级审批人接口
    public function threeLevel() {
    	$fields = 'id, nickname';
		$where  = [
					'role_id' => '分管副总'
				];
    	$result = $this->user_model->findData($fields, $where);       
        echo json_encode($result,JSON_UNESCAPED_UNICODE);
    }
	
	// 四级审批人接口
    public function fourLevel(){  
    	$fields = 'id, nickname';
		$where  = [
					'role_id' => '财务分管副总'
				];
    	$result = $this->user_model->findData($fields, $where);    
        echo json_encode($result, JSON_UNESCAPED_UNICODE);
    }

    // 五级审批人接口
    public function fiveLevel(){
    	$fields = 'id, nickname';
		$where  = [
					'role_id' => '董事长'
				];
    	$result = $this->user_model->findData($fields, $where);
        echo json_encode($result, JSON_UNESCAPED_UNICODE);

    }

    // 财务审批抄送人接口
    public function ccLevel(){
        $fields = 'id, nickname';
		$where  = [
					'role_id' => '财务主管'
				];

        $result = $this->user_model->findData($fields, $where);
        echo json_encode($result, JSON_UNESCAPED_UNICODE);
    }

    // 接口
    public function editOneLevel(){
    	$id = $this->input->post('id');
		
		
        $arg = '主管';
        $result = $this->user_model->FindInSet($arg);
		
        //查询当前选中的值
        $arg2 = [
            'id' => $_POST['id']
        ];
		
        $type = $_POST['type'];
        if($type == '普通需求单' || $type == '采购项目单' || $type == '招投标项目需求单'){
            $presentValue = $this->procurement_model->getByFieldQueryData('director',$arg2,0);
            $result['presentValue'] = $presentValue['director'];
        }elseif ($type == '采购退货单' || $type == '采购换货单'){
            $presentValue = $this->returnOrder_model->getByFieldQueryData('director',$arg2,0);
            $result['presentValue'] = $presentValue['director'];
        }elseif ($type == '商品出库单'){
            $presentValue = $this->stockOut_model->getByFieldQueryData('director',$arg2,0);
            $result['presentValue'] = $presentValue['director'];
        }elseif ($type == '商品入库单'){
            $presentValue = $this->stockPut_model->getByFieldQueryData('director',$arg2,0);
            $result['presentValue'] = $presentValue['director'];
        }

        echo json_encode($result,JSON_UNESCAPED_UNICODE);
    }
    public function editTwoLevel(){
        $arg = '分管副总';
        $result = $this->user_model->FindInSet($arg);

        //查询当前选中的值
        $arg2 = [
            'id' => $_POST['id']
        ];

        $type = $_POST['type'];
        if($type == '普通需求单' || $type == '采购项目单' || $type == '招投标项目需求单'){
            $presentValue = $this->procurement_model->getByFieldQueryData('president',$arg2,0);
            $result['presentValue'] = $presentValue['president'];
        }elseif ($type == '采购退货单' || $type == '采购换货单'){
            $presentValue = $this->returnOrder_model->getByFieldQueryData('president',$arg2,0);
            $result['presentValue'] = $presentValue['president'];
        }elseif ($type == '商品出库单'){
            $presentValue = $this->stockOut_model->getByFieldQueryData('president',$arg2,0);
            $result['presentValue'] = $presentValue['president'];
        }elseif ($type == '商品入库单'){
            $presentValue = $this->stockPut_model->getByFieldQueryData('president',$arg2,0);
            $result['presentValue'] = $presentValue['president'];
        }



        echo json_encode($result,JSON_UNESCAPED_UNICODE);
    }
    public function editThreeLevel(){
        $arg = '董事长';
        $result = $this->user_model->FindInSet($arg);

        //查询当前选中的值
        $arg2 = [
            'id' => $_POST['id']
        ];

        $type = $_POST['type'];
        if($type == '普通需求单' || $type == '采购项目单' || $type == '招投标项目需求单'){
            $presentValue = $this->procurement_model->getByFieldQueryData('chairman',$arg2,0);
            $result['presentValue'] = $presentValue['chairman'];
        }elseif ($type == '采购退货单' || $type == '采购换货单'){
            $presentValue = $this->returnOrder_model->getByFieldQueryData('chairman',$arg2,0);
            $result['presentValue'] = $presentValue['chairman'];
        }


        echo json_encode($result,JSON_UNESCAPED_UNICODE);
    }
    public function editCCLevel(){
        $arg = '财务';
        $result = $this->user_model->FindInSet($arg);

        //查询当前选中的值
        $arg2 = [
            'id' => $_POST['id']
        ];

        $type = $_POST['type'];
        if($type == '普通需求单' || $type == '采购项目单' || $type == '招投标项目需求单'){
            $presentValue = $this->procurement_model->getByFieldQueryData('financial',$arg2,0);
            $result['presentValue'] = $presentValue['financial'];
        }elseif ($type == '采购退货单' || $type == '采购换货单'){
            $presentValue = $this->returnOrder_model->getByFieldQueryData('financial',$arg2,0);
            $result['presentValue'] = $presentValue['financial'];
        }
        echo json_encode($result,JSON_UNESCAPED_UNICODE);
    }



}